#include "StdAfx.h"
#include "user_mgr_impl.h"
#include "global_data.h"
#include <interface/data_op/table_wrapper.h>
#include <interface/interface.h>
#include <utility/api_tools.h>
#include <interface/data_op/userinfo.h>


namespace aot{ namespace glb{   

user_mgr_impl::user_mgr_impl(void)
{
    this->lock_.open();
	user_balance_ = 0.00;
	ent_balance_ = 0.00;
	user_info = NULL;
	g_dll_obj->query_interface_from_data_op_dll((void**)&user_info, "iuser_info");
    assert(user_info);
}

user_mgr_impl::~user_mgr_impl()
{
	if (user_info)
	{
		user_info->destroy();
	}
    this->lock_.close();
}

bool 
user_mgr_impl::save_to_db()
{
   if ( user_info )
   {
       aot::tt::iobj_auto_ptr<aot::data::iuser_op> user_op;
       if( !g_dll_obj->query_interface_from_data_op_dll(user_op, "iuser_op") )
           return false;

       if ( !user_op->is_info_exist(user_info->user_id->c_str()) )
       {
           return user_op->insert(user_info);
       }
       else
       {
           return user_op->update(user_info);
       }
   }
   return false;
}

bool 
user_mgr_impl::load_from_db()
{
    if ( NULL == user_info )
    {
        return false;
    }

    if ( user_info->user_id->length() == 0 )
    {
        return false;
    }

    aot::tt::iobj_auto_ptr<aot::data::iuser_op> user_op;
    if( !g_dll_obj->query_interface_from_data_op_dll(user_op, "iuser_op") )
        return false;

    if ( user_op->is_info_exist(user_info->user_id->c_str()) )
    {
        return user_op->read(user_info->user_id->c_str(), user_info);
    }
    
    return false;
}

}} /* end namespace aot/glb*/